﻿using AppDataStruct;
using ComUIControl;
using EECMISWS;
using KWindow.KPageCore;
using KXmlUIControl.KControl;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageApplyTWQZ : PageControlBase
    { 
        private PreApplyInfo _appInfo = null;

        private Button buttonOK = null;
        private Button buttonCancell = null;

        private List<KCheckBox> listQZLXCheckBoxs = new List<KCheckBox>();

        public PageApplyTWQZ()
        {
            InitializeComponent();
            SetProcessBase(ProcessApplyTWQZ.Instance);
        }

        private void GetQZLXCheckBoxs()
        {
            List<Control> list = dicRootGroup["groupTWQZLX"];
            foreach (Control ctl in list)
            {
                if (ctl is KCheckBox)
                {
                    listQZLXCheckBoxs.Add(ctl as KCheckBox);
                }
            }         
        }

        private bool IsLongTimeQZ(KCheckBox box)
        {
            List<Control> list = dicSubGroup[box];
            if (list == null || list.Count == 0)
            {
                return true;
            }
            return false;
        }

        private List<KCheckBox> GetQZZLCheckBoxs(KCheckBox box)
        {
            List<KCheckBox> listBoxs = new List<KCheckBox>();
            List<Control> list = dicSubGroup[box];
            if(list == null || list.Count == 0)
            {
                return listBoxs;
            }
            foreach (Control ctl in list)
            {
                if (ctl is KCheckBox)
                {
                    listBoxs.Add(ctl as KCheckBox);
                }
            }
            return listBoxs;
        }

        private void GetControls()
        {
            buttonOK = GetControl<Button>("buttonOK") as Button;
            buttonCancell = GetControl<Button>("buttonCancell") as Button;
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            InitKeyboards();
            base.InitCtrls();
            GetControls();
            GetQZLXCheckBoxs();        
            SetButtonDefaultStyles(buttonOK);
            SetButtonDefaultStyles(buttonCancell);
        }

        private void InitCheckBoxs()
        {
            KCheckBox boxChecked = null;
            if(string.IsNullOrEmpty(_appInfo.DLZSQ.QZZL.Code))
            {
                SetDefaultControlCheckedInGroup("groupTWQZLX");
                return;
            }

            int index = listQZLXCheckBoxs.FindIndex(r => _appInfo.DLZSQ.QZZL.Code == (string)r.Tag);
            if (index >= 0)
            {
                listQZLXCheckBoxs[index].Checked = true;
                boxChecked = listQZLXCheckBoxs[index];
            }
            else
            {
                listQZLXCheckBoxs[0].Checked = true;
                boxChecked = listQZLXCheckBoxs[0];
            }
            if (boxChecked != null && !IsLongTimeQZ(boxChecked))
            {
                if (!string.IsNullOrEmpty(_appInfo.DLZSQ.YXCS.Code)
               && !string.IsNullOrEmpty(_appInfo.DLZSQ.YXQX.Code))
                {
                    List<KCheckBox> listQZZL = GetQZZLCheckBoxs(boxChecked);
                    string qzzl = _appInfo.DLZSQ.YXQX.Code + "_" + _appInfo.DLZSQ.YXCS.Code;
                    index = listQZZL.FindIndex(r => qzzl == (string)r.Tag);
                    if (index >= 0)
                    {
                        listQZZL[index].Checked = true;
                    }
                }
            }            
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");
            if(_appInfo != null)
            {
                InitCheckBoxs();
            }
            if (_appInfo == null)
            {
                _appInfo = new PreApplyInfo();
            }
        }

        private bool CheckUserCheckedIsOK()
        {
            int count = 0;
            KCheckBox checkedBox = null;
            foreach (KCheckBox box in listQZLXCheckBoxs)
            {
                if (box.Checked)
                {
                    checkedBox = box;
                    count++;
                }
            }
            if (count == 0)
            {
                MsgBox.Show("请选择签注类型.");
                return false;
            }
            else if (count > 1)
            {
                MsgBox.Show("系统错误: 选择了多个签注类型.");
                return false;
            }

            if(IsLongTimeQZ(checkedBox))
            {
                return true;
            }
            List<KCheckBox> listQZZL = GetQZZLCheckBoxs(checkedBox);
            count = 0;
            foreach (KCheckBox box in listQZZL)
            {
                if (box.Checked)
                {
                    count++;
                }
            }
            if (count == 0)
            {
                MsgBox.Show("请选择签注种类.");
                return false;
            }
            else if (count > 1)
            {
                MsgBox.Show("系统错误: 选择了多个签注种类.");
                return false;
            }
            return true;
        }

        private void UpdateQZInfo()
        {
            KCheckBox checkedBox = listQZLXCheckBoxs.Find(r => r.Checked);
            _appInfo.DLZSQ.SFQZ.Code = "1";
            _appInfo.DLZSQ.SFQZ.Value = "是";
            _appInfo.DLZSQ.QZZL.Code = (string)checkedBox.Tag;
            _appInfo.DLZSQ.QZZL.Value = checkedBox.Title;
            if(IsLongTimeQZ(checkedBox))
            {
                _appInfo.DLZSQ.YXQX.Code = "N01";
                _appInfo.DLZSQ.YXQX.Value = "一年内"; 
                _appInfo.DLZSQ.YXCS.Code = "9";
                _appInfo.DLZSQ.YXCS.Value = "多次有效";
                return;
            }

            List<KCheckBox> listQZZL = GetQZZLCheckBoxs(checkedBox);
            KCheckBox qzzlBox = listQZZL.Find(r => r.Checked);
            string qzzl = (string)qzzlBox.Tag;
            if(string.IsNullOrEmpty(qzzl))
            {
                MessageBox.Show("无效的签注种类代码.");
                return;
            }
            string[] array = qzzl.Split('_');
            CodeMapService service = CodeMapService.Instance;
            if (array.Length == 2)
            {
                _appInfo.DLZSQ.YXQX.Code = array[0];
                _appInfo.DLZSQ.YXQX.Value = service["YXQ", array[0]];
                _appInfo.DLZSQ.YXCS.Code = array[1];
                _appInfo.DLZSQ.YXCS.Value = service["QZYXCS", array[1]];
            }
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            UpdateQZInfo();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            foreach(KCheckBox box in CheckBoxs)
            {
                box.Checked = false;
            }
            _appInfo = null;
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("用户操作超时，返回首页");
            base.OnCountdownCompleted(sender, e);
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            base.btnCancel_Click(sender, e);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckUserCheckedIsOK())
            {
                return;
            }
            StopCountdown();
            UpdateData();
            ClearData();
            Process.ToAny("YSLPages.PageSelectDocumentType");           
        }

        private void btnCancell_Click(object sender, EventArgs e)
        {
            StopCountdown();
            ClearData();
            Process.ToAny("YSLPages.PageApplyTWZInfo");
        }
    }
}
